package com.byb.server.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.WcClainPartyService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.WcClainPartyDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing WcClainParty.
 */
@RestController
@RequestMapping("/api")
public class WcClainPartyResource {

    private final Logger log = LoggerFactory.getLogger(WcClainPartyResource.class);

    private static final String ENTITY_NAME = "wcClainParty";

    private final WcClainPartyService wcClainPartyService;

    public WcClainPartyResource(WcClainPartyService wcClainPartyService) {
        this.wcClainPartyService = wcClainPartyService;
    }

    /**
     * POST  /wc-clain-parties : Create a new wcClainParty.
     *
     * @param wcClainPartyDTO the wcClainPartyDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new wcClainPartyDTO, or with status 400 (Bad Request) if the wcClainParty has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/wc-clain-parties")
    @Timed
    public ResponseEntity<WcClainPartyDTO> createWcClainParty(@RequestBody WcClainPartyDTO wcClainPartyDTO) throws URISyntaxException {
        log.debug("REST request to save WcClainParty : {}", wcClainPartyDTO);
        if (wcClainPartyDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new wcClainParty cannot already have an ID")).body(null);
        }
        WcClainPartyDTO result = wcClainPartyService.save(wcClainPartyDTO);
        return ResponseEntity.created(new URI("/api/wc-clain-parties/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /wc-clain-parties : Updates an existing wcClainParty.
     *
     * @param wcClainPartyDTO the wcClainPartyDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated wcClainPartyDTO,
     * or with status 400 (Bad Request) if the wcClainPartyDTO is not valid,
     * or with status 500 (Internal Server Error) if the wcClainPartyDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/wc-clain-parties")
    @Timed
    public ResponseEntity<WcClainPartyDTO> updateWcClainParty(@RequestBody WcClainPartyDTO wcClainPartyDTO) throws URISyntaxException {
        log.debug("REST request to update WcClainParty : {}", wcClainPartyDTO);
        if (wcClainPartyDTO.getId() == null) {
            return createWcClainParty(wcClainPartyDTO);
        }
        WcClainPartyDTO result = wcClainPartyService.save(wcClainPartyDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, wcClainPartyDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /wc-clain-parties : get all the wcClainParties.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of wcClainParties in body
     */
    @GetMapping("/wc-clain-parties")
    @Timed
    public ResponseEntity<List<WcClainPartyDTO>> getAllWcClainParties(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of WcClainParties");
        Page<WcClainPartyDTO> page = wcClainPartyService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/wc-clain-parties");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /wc-clain-parties/:id : get the "id" wcClainParty.
     *
     * @param id the id of the wcClainPartyDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the wcClainPartyDTO, or with status 404 (Not Found)
     */
    @GetMapping("/wc-clain-parties/{id}")
    @Timed
    public ResponseEntity<WcClainPartyDTO> getWcClainParty(@PathVariable Long id) {
        log.debug("REST request to get WcClainParty : {}", id);
        WcClainPartyDTO wcClainPartyDTO = wcClainPartyService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(wcClainPartyDTO));
    }

    /**
     * DELETE  /wc-clain-parties/:id : delete the "id" wcClainParty.
     *
     * @param id the id of the wcClainPartyDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/wc-clain-parties/{id}")
    @Timed
    public ResponseEntity<Void> deleteWcClainParty(@PathVariable Long id) {
        log.debug("REST request to delete WcClainParty : {}", id);
        wcClainPartyService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/wc-clain-parties?query=:query : search for the wcClainParty corresponding
     * to the query.
     *
     * @param query the query of the wcClainParty search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/wc-clain-parties")
    @Timed
    public ResponseEntity<List<WcClainPartyDTO>> searchWcClainParties(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of WcClainParties for query {}", query);
        Page<WcClainPartyDTO> page = wcClainPartyService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/wc-clain-parties");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
